/**
 * @ author: 汪孟苹
 */
define(["jquery","template","dg/datagride.common","dg/datagride.attention","fu/app/comm/comm","fu/app/question/question.ui"],
	function($,template,$common,$attention,$comm,$questionUi){
	var quest = {};

	/**
	 * 数据处理: 该方法根据题型实际出发
	 * quest.formatData
	 *    1、不需要可以不写
	 * 	  2、若只有content需要处理数据，可以将contentFormat里的数据处理直接写在该方法里；
	 * 	  		quest.formatData = function(obj){
	 * 	  			// doing
	 * 	  			return obj;
	 * 	  		};
	 * 	  3、若content和head都需要处理数据，则quest.forma的typeof是对象object：
	 * 	  		quest.formatData = {
	 * 	  			"head": headFormat,
	 * 	  			"content": contentFormat
	 * 	  		};
	 * 	  		var headFormat = function(obj){};
	 * 	  		var contentFormat = function(obj){};
	 * 	   		注意：headFormat:返回处理后的head数据；
	 * 	   	 		  contentFormat:返回处理后的content数据；
	 */
	quest.formatData = function(newObj){
    	if( newObj.list && typeof newObj.list != "string" ){
			newObj.list = JSON.stringify(newObj.list);
		}
		return newObj;
	}

	/**
	 * tpl模板
	 * quest.tpl
	 * 		1、若是组类的题型，quest.tpl的返回值得typeof是string，即非组题型里的tpl_label;
	 * 		2、若是非组类的题型，quest.tpl的返回值得typeof是object；
	 */
	var tpl_label = function(){
		return  '<script id="tpl_modal_label" type="text/html">'+
				'	<div style="{{label.style}}">{{label.value}}</div>'+
				'</script>';
	}
	var tpl_input = function(){
		return '<script id="tpl_modal_input" type="text/html">'+
				'	<div class="datagride-modal validatebox" style="{{style}}" data-questionId="{{questionId}}" data-recordId="{{recordId}}" data-list="{{list}}" data-attentionType="{{attentionType}}" data-attention="{{attention}}">{{value}}</div>'+
				'</script>';
	}
	quest.tpl = function(){
		var tpl = {};
		tpl.label = tpl_label;
		tpl.input = tpl_input;
		return tpl;
	}

	/**
	 * 事件: 该方法根据题型实际出发
	 * quest.event
	 *    1、不需要可以不写
	 * 	  2、若只有content需要事件处理，可以将contentEvent里的事件处理直接写在该方法里；
	 * 	  		quest.event = function(params){
	 * 	  			// doing
	 * 	  		};
	 * 	  3、若content和head都需要事件处理，则quest.event的typeof是对象object：
	 * 	  		quest.event = {
	 * 	  			"head": headEvent,
	 * 	  			"content": contentEvent
	 * 	  		};
	 * 	  		var headEvent = function(params){};
	 * 	  		var contentEvent = function(params){};
	 * 	   		注意：headEvent:处理head里的事件；
	 * 	   	 		  contentEvent:处理content里的事件；
	 */
	// 弹出的模态框模板
	var tpl_modal = function(){
		return  '<script id="tpl_modal" type="text/html">'+
				'	<div class="datagrideModal">'+
				'		<ul class="datagrideModal-tit clearfix">'+
				'			{{each list as value i}}'+
				'			<li data-url="{{value.url}}">{{value.text}}</li>'+
				'			{{/each}}'+
				'		</ul>'+
				'		<div id="datagrideModalContent" class="datagrideModal-content"></div>'+
				'		<div class="datagrideModal-btn">'+
				'			<button type="button" class="btn datagrideModal-btn-confirm">确定</button>'+
				'			<button type="button" class="btn datagrideModal-btn-cancel">取消</button>'+
				'		</div>'+
				'	</div>'+
		        '</script>';
	}
	// 弹出的模态框初始化
	var modalInit = function(options){
		$("." + options.className + " .modal-body").html( tpl_modal() );
		var html = template("tpl_modal",options);
		$("." + options.className + " .modal-body").html(html);
	}
	// 暂时数据（实际是要请求的）
	var question = [[
		{
		    "label": "GESELL量表",
		    "showLabel": false,
		    "type": "qusetion_group",
		    "children": [
		        {
		            "id": "YFY98F44666D",
		            "label": "动作能DA",
		            "name": "exerDa",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0",
		            "type": "question_text"
		        },
		        {
		            "id": "YFY984DB66B8",
		            "label": "动作能DQ",
		            "name": "exerDq",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0,integer:true",
		            "type": "question_text"
		        },
		        {
		            "id": "YFY984DB66B1",
		            "label": "动作能评价",
		            "name": "exeiAssess",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYA406EE065",
		            "label": "应物能DA",
		            "name": "ywnDa",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0",
		            "type": "question_text"
		        },
		        {
		            "id": "YFY44F8A67B3",
		            "label": "应物能DQ",
		            "name": "ywnDq",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0,integer:true",
		            "type": "question_text"
		        },
		        {
		            "id": "YFY44F8A67B9",
		            "label": "应物能评价",
		            "name": "ywnAssess",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "type": "question_text"
		        },
		        {
		            "id": "YFY624BD8E1E",
		            "label": "言语能DA",
		            "name": "langDa",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYAD4543327",
		            "label": "言语能DQ",
		            "name": "langDq",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0,integer:true",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYAD4543328",
		            "label": "言语能评价",
		            "name": "langAssess",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYA42651DC1",
		            "label": "应人能DA",
		            "name": "ynrDa",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYCDFA2DADD",
		            "label": "应人能DQ",
		            "name": "ynrDq",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "validateRule":"noNull:false,num:true,moreThanNum:0,integer:true",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYCDFD2DADD",
		            "label": "应人能评价",
		            "name": "ynrAssess",
		            "notNull": true,
		            "length": 4,
		            "placeHolder": "",
		            "type": "question_text"
		        },
		        {
		            "id": "YFYCDFA2DAD2",
		            "label": "初步诊断",
		            "name": "preDiagnose",
		            "notNull": true,
		            "length": 12,
		            "placeHolder": "",
		            "type": "question_textarea"
		        },
		        {
		            "id": "YFYCDFA4332D",
		            "label": "处理意见",
		            "name": "dealSugg",
		            "notNull": true,
		            "length": 12,
		            "placeHolder": "",
		            "type": "question_textarea"
		        }
		    ]
		}
	]];
	// 切换tab
	var onTabSelect = function(){
		if( !$(this).hasClass("active") ){
    		$(this).addClass("active").siblings().removeClass("active");
    		var url = $(this).attr("data-url");
    		$questionUi.init({
	        	id: "datagrideModalContent",
	            list: question,	  // 问卷
	        });
    	}
	}
	// 事件
	quest.event = function(params){
		$(".datagride-modal").unbind("click");
		$(".datagride-modal").click(function(){
			var listStr = $(this).attr("data-list");
			if( listStr ){
				var list = JSON.parse( listStr );
				var className = "datagrideModalModal";
				$comm.modal({
					"className": className,
					"tit":""
				},function(){
					modalInit({
						"className":className,
						"list":list
					});
					$("." + className).modal('show');
			        $(".datagrideModal-tit li").unbind("click");
			        $(".datagrideModal-tit li").click(onTabSelect);
			        $(".datagrideModal-tit li").first().click();
		        });
			}
		});
	}

	return quest;
});